package com.zfl.controller;

import com.zfl.mapper.ArticleMapper;
import com.zfl.pojo.Article;
import com.zfl.pojo.PageBean;
import com.zfl.pojo.Result;
import com.zfl.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/api/article")
@CrossOrigin
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @PutMapping
    public Result update(@RequestBody /*@Validated*/ Article article) {
        articleService.upadte(article);
        return Result.success();
    }

    @DeleteMapping
    public Result delete(int id) {
        articleService.delete(id);
        return Result.success();
    }

    @GetMapping("/detail")
    public Result<Article> detail(int id)  {
        Article article = articleService.detail(id);
        return Result.success(article);
    }

    @PostMapping
    public Result add(@RequestBody /*@Validated*/ Article article) {
        articleService.add(article);
        return Result.success();
    }

    @GetMapping
    public Result<PageBean<Article>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state) {
        PageBean<Article> pb = articleService.list(pageNum,pageSize,categoryId,state);
        return Result.success(pb);

    }
}
